# 基础效果 应用HP变化 增加或减少 可设定时间 时间越长 单位时间诶恢血量越低
class_name  HpChangeEffective
extends EffectiveBase

# 血量的变化量
@export var hp_delta:float = 10
# 恢复时间
@export var time:float = 0 

# 定时器精度 多长时间结算一次
@export var timer_timeout:float = 0.2

var hp_in_one_timer_tick: float 
var hp_reset:float = hp_delta
var timer:Timer = null


func _ready():
	pass	
		
		

func name():
	return "hp_change_effective"
	
func _apply_hp(hp_v:float) :
	var hp = character.get(Character.HP)
	print("set hp , hp  ", hp + hp_v)
	character.set(Character.HP, hp+hp_v)


func apply():	
	print("apply ", name())
	hp_reset = hp_delta
	if is_zero_approx(time):
		if character :
			_apply_hp(hp_delta)
			effective_finished.emit()
	else :
		hp_in_one_timer_tick = hp_delta / (time/timer_timeout)
		timer = Timer.new()
		add_child(timer)
		timer.start(timer_timeout)
		timer.timeout.connect(
			func():
				var rest = hp_reset - hp_in_one_timer_tick
				#print("rest = ", rest, " hp rest  = ", hp_reset)
				if rest >= 0 :
					_apply_hp(hp_in_one_timer_tick)
					hp_reset -= hp_in_one_timer_tick
					#print("aaa rest = ", rest, " hp rest  = ", hp_reset)
				else:
					_apply_hp(hp_reset)
					timer.stop()
					effective_finished.emit()
					#print("bbb rest = ", rest, " hp rest  = ", hp_reset)
				)
		
